<template>
  <div>
    <home-header></home-header>
    <home-swiper :swiperList='swiperList'></home-swiper>
    <home-icons :iconList='iconList'></home-icons>
    <home-recommend :recommendList='recommendList'></home-recommend>
    <home-weekend :weekendList='weekendList'></home-weekend>
  </div>
</template>
<script>
import HomeHeader from './components/Header'
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import HomeWeekend from './components/Weekend'
import axios from 'axios'
import { mapState } from 'vuex'
/* eslint-disable no-new */
export default {
  name: 'Home',
  data () {
    return {
      lastCity: '', // 临时缓冲量
      swiperList: [],
      iconList: [],
      recommendList: [],
      weekendList: []
    }
  },
  computed: { // 定义计算属性获取vuex中的首页面的城市
    ...mapState(['city'])
  },
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  methods: {
    getHomeInfo () {
      axios.get('/api/index.json?city=' + this.city).then(this.getHomeInfoSucc)
    },
    getHomeInfoSucc (res) {
      res = res.data
      if (res.ret && res.data) {
        this.swiperList = res.data.swiperList
        this.iconList = res.data.iconList
        this.recommendList = res.data.recommendList
        this.weekendList = res.data.weekendList
      }
    }
  },
  mounted () { // 页面被加载时发送一次ajax请求
    this.lastCity = this.city // 第一次加载时对当前的城市做一次保存
    this.getHomeInfo()
  },
  activated () { // 页面返回时就执行一次activated函数钩子
    if (this.lastCity !== this.city) {
      this.lastCity = this.city // 如果选择了新城市与原城市不一样就重新发送一次ajax请求，并且把新城市赋给lastcity
      this.getHomeInfo()
    }
  }
}

</script>
<style>
</style>
